import { Component, OnInit } from '@angular/core';
import {MyModalHelper} from "@core/my-modal.helper";
import {AgentService} from "@core/routes-services/agent.service";
import {myValidNull} from "@core/util/string-utils";
import {NzMessageService} from "ng-zorro-antd";
import {AgentLevelRelateComponent} from "../agent-level-relate/agent-level-relate.component";

@Component({
  selector: 'app-agent-director',
  templateUrl: './director.component.html',
})
export class AgentDirectorComponent implements OnInit {

  isLoading = false;
  keyword: any;
  type:any = 1;//1:市级渠道
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  levelOptions: any [] = [];
  agentList:any [] = [];

  constructor(private message: NzMessageService,
              private agentService:AgentService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initAgentList();
    this.initPage();
  }

  initAgentList(){
    //4 市级代理商
    this.agentService.getAllBylevel(4).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.agentList = res.data;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  initPage() {
    this.isLoading = true;
    this.agentService.relatePagination(this.page, this.keyword, this.type).then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(AgentLevelRelateComponent, { entity: bak, agentList: this.agentList,type: this.type}, 560, {
      nzTitle: '招执行董事',
    }).subscribe(result => {
      if(result){
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };



}
